<?php


namespace app\admin\controller;


use app\common\controller\Adminbase;
use think\Db;

class Teamji extends Adminbase
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function index()
    {
        if ($this->request->isAjax()) {
            list($page, $limit, $where) = $this->buildTableParames();
            $_list = Db::name('member')
                ->where($where)
                ->order('create_time')
                ->page($page, $limit)
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                $_list[$k]['level'] = Db::name('member_level')->find($v['level_id']);
                if (!$v['code_url']) {
                    $code_url = qrcode($v['id'], '');
                    Db::name('member')->update([
                        'id' => $v['id'],
                        'code_url' => $code_url
                    ]);
                    $_list[$k]['code_url'] = $code_url;
                }
                //团队人数
                $team_ids = getTeamMembersRecursive($v['id']);
                array_unshift($team_ids, $v['id']);

                $_list[$k]['team_num'] = count($team_ids);
                //总业绩
                $_list[$k]['zong_yeji'] = round(teamMoneySum($v['id']), 2);
            }
            $total = Db::name('member')
                ->where($where)
                ->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        return $this->fetch();
    }

    public function detail()
    {

        $finance_id = $this->request->param('id/d', 0);
        $data = Db::name('team_money')->find($finance_id);

        $data['create_time'] = date('Y-m-d H:i:s', $data['create_time']);

        if ($data['send_time']) {
            $data['send_time'] = date('Y-m-d H:i:s', $data['send_time']);
        } else {
            $data['send_time'] = '待发送';
        }

        $this->assign("data", $data);
        $this->assign("is_detail", 1);
        return $this->fetch('teammoney/edit');
    }

    public function ckuan()
    {
        $id = $this->request->param('id/d', null);
        $finance_info = Db::name('team_money')->find($id);

        if ($finance_info['is_kuan'] == 1) {
            $this->error('当前已转账！');
        }
        $update = array(
            'id' => $id,
            'send_time' => time(),
            'is_send' => 1
        );
        $result = Db::name('team_money')->update($update);
        if ($result) {
            return ['code' => 200, 'data' => '', 'msg' => '操作成功'];
        } else {
            return ['code' => 100, 'data' => '', 'msg' => '操作失败'];
        }
    }

    //增加线下业绩
    public function recharge()
    {
        if ($this->request->isPost()) {
            $data = $this->request->Post();
            $insert_data = array(
                'member_id' => $data['id'],
                'cyeji' => $data['cyeji']
            );
            $cyeji = Db::name('member')
                ->where('id', $data['id'])
                ->setInc('cyeji', $data['cyeji']);
            $c = Db::name('member_yeji')->insertGetId([
                'member_id' => $data['id'],
                'money' => $data['cyeji'],
                'create_time' => time()
            ]);
            if ($c) {
                $this->success('完成');
            } else {
                $this->error('失败！');
            }
            return $data;
        }
        $id = $this->request->param('id/d', 0);
        $data = Db::name('member')->find($id);
        $this->assign('data', $data);
        return $this->fetch();
    }

    public function yejic()
    {
        $data = $this->request->param();
        if ($this->request->isAjax()) {
            $where[] = ['id', '=', $data['member_id']];
            $member = Db::name('member')->where($where)->find();
            $_list = array();
            $date_list = generateYearMonthArray($member['create_time']);
            foreach ($date_list as $k => $v) {
                $_list[$k]['id'] = $k + 1;
                $_list[$k]['date'] = $v;
                $team_ids = getTeamMembersRecursive($data['member_id']);
                array_unshift($team_ids, $data['member_id']);
                $team_money = getEffectOrderMoney($team_ids, strtotime($v . '-01 00:00:00'), strtotime('last day of ' . $v . ' 23:59:59'));
                $_list[$k]['team_money'] = $team_money;
                $_list[$k]['zong_team_money'] = round($team_money, 2);
            }
            $result = ["code" => 0, "count" => count($_list), "data" => $_list];
            return json($result);
        }
        $this->assign('member_id', $data['id']);
        return $this->fetch();
    }

    public function team()
    {
        $data = $this->request->param();
        if ($this->request->isAjax()) {
            list($page, $limit, $where) = $this->buildTableParames();
            $team_ids = getTeamMembersRecursive($data['member_id']);
            array_unshift($team_ids, (int)$data['member_id']);
            $where2[] = ['id', 'in', $team_ids];
            $_list = Db::name('member')
                ->where($where2)
                ->order('create_time')
                ->page($page, $limit)
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['level'] = Db::name('member_level')->find($v['level_id']);
                $team_ids = getTeamMembersRecursive($v['id']);
                array_unshift($team_ids, $v['id']);

                $_list[$k]['team_num'] = count($team_ids);
                $yeji2 = getEffectOrderMoneySum($v['id']);
                $_list[$k]['zong_yeji'] = round($yeji2, 2);
            }
            $total = Db::name('member')
                ->where($where2)
                ->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $this->assign('member_id', $data['id']);
        return $this->fetch();
    }

    public function yejilist()
    {
        $data = $this->request->param();
        if ($this->request->isAjax()) {
            list($page, $limit) = $this->buildTableParames();
            $team_ids = getTeamMembersRecursive($data['member_id']);
            array_unshift($team_ids, (int)$data['member_id']);

            $where2[] = ['o.member_id', 'in', $team_ids];
            $where2[] = ['o.status', 'in', [4, 5]];
            $where2[] = ['o.pay_type', '=', 1];

            $filter = input('filter');
            if ($filter) {
                $filter = json_decode($filter, true);
                if (isset($filter['order_no'])) {
                    $where[] = ['o.order_no', 'like', '%' . $filter['order_no'] . '%'];
                }
                if (isset($filter['finish_time'])) {
                    $create_time = explode(' - ', $filter['finish_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['o.finish_time', 'between', [$create_time[0], $create_time[1]]];
                }
                if (isset($filter['create_time'])) {
                    $create_time = explode(' - ', $filter['create_time']);
                    foreach ($create_time as $k => $v) {
                        $create_time[$k] = strtotime($v);
                    }
                    $where[] = ['o.create_time', 'between', [$create_time[0], $create_time[1]]];
                }
                if (isset($filter['nickname'])) {
                    $where[] = ['m.nickname', 'like', '%' . $filter['nickname'] . '%'];
                }

                if (isset($filter['mobile'])) {
                    $where[] = ['m.mobile', 'like', '%' . $filter['mobile'] . '%'];
                }
            }
            $_list = Db::name('order')->alias('o')
                ->join('member m', 'o.member_id=m.id')
                ->field('o.*,m.nickname,m.avatarUrl,m.mobile')
                ->where($where2)
                ->where($where)
                ->order('o.finish_time desc')
                ->page($page, $limit)
                ->select();
            foreach ($_list as $k => $v) {
                $_list[$k]['finish_time'] = date('Y-m-d H:i:s', $v['finish_time']);
                $_list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
            }
            $total = Db::name('order')->alias('o')
                ->join('member m', 'o.member_id=m.id')
                ->field('o.*,m.nickname,m.avatarUrl,m.mobile')
                ->where($where)
                ->where($where2)
                ->count();
            $result = ["code" => 0, "count" => $total, "data" => $_list];
            return json($result);
        }
        $this->assign('member_id', $data['id']);
        return $this->fetch();
    }

    public function goods_list()
    {
        $id = $this->request->param('id/d', 0);
        $goods_list = Db::name('order_goods')
            ->where(['order_id' => $id])
            ->order('sort desc')
            ->select();
        foreach ($goods_list as $k => $v) {
            $goods_info = Db::name('goods')->find($v['goods_id']);
            $goods_list[$k]['goods_info'] = $goods_info;
        }
        $this->assign([
            'goods_list' => $goods_list
        ]);
        return $this->fetch('server');
    }
    

}